package com.hx.company.controller;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.jni.OS;
import org.apache.tomcat.jni.Thread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hx.company.UserInterface;
import com.hx.company.service.MerchantInterfaceImple;
import com.hx.company.vo.Merchant;
import com.hx.company.vo.Order;
import com.hx.company.vo.PicName;
import com.hx.company.vo.User;
import com.hx.company.vo.UserLogin;




@Controller
public class UserMangerController {
	@Autowired(required=true)
	private MerchantInterfaceImple mif;
	@Autowired(required=true)
	private  UserInterface ui;
	
	
	
	@RequestMapping("/test")
	@ResponseBody
	public User test11(){
		User s = new User();
		s.setUaddress("adress");
		s.setUname("join");
		s.setUphone("123");
		return s;
	}
	
	@RequestMapping("/getpic")
	public void getpic(HttpServletRequest request,HttpServletResponse response) throws Exception{
	
		 OutputStream out = response.getOutputStream();
		 File file = new File("C:/Users/PC/Desktop/qq.jpg");
		 FileInputStream  fis = new FileInputStream(file);
		 byte[] b = new byte[fis.available()];
	        fis.read(b);
	        out.write(b);
	        out.flush();
	        fis.close();
	        out.close();
	}
	
	@RequestMapping("/getpicname")
	public void getPicName(String name,HttpServletRequest request){
		PicName pn=new PicName();
		pn.setName(name);
		pn.setAddress("c:/");
		ui.addPicName(pn);
	}
	
	@RequestMapping("/testpic")
	public void testpic(HttpServletRequest request,HttpServletResponse response) throws IOException{
		
		String name=mif.getPicName();
		System.out.println(name+"----");
		//String name="1234.jpg";
		InputStream is= request.getInputStream();
		String path="C:/Users/PC/Desktop/";
		File file=new File(path+name+".jpg");
		if(!file.exists()){
		file.createNewFile();}
		FileOutputStream os = new FileOutputStream(file);
		byte[] buffer = new byte[1024];
		int length = 0 ;
		while((length = is.read(buffer))>0){
			os.write(buffer, 0, length);
		}
		is.close();
		os.close();
		
	}
	
	@RequestMapping("/userRegist")
	public String uesrRegist(User u,UserLogin ul,HttpServletRequest request){
		System.out.println(u.getUname()+u.getUphone()+"........"+ul.getUphone()+ul.getUpassword());
		int rs=ui.userRegister(u,ul);	
		if(rs>0)
		return "redirect:/login.jsp";
		else return "regist1.jsp";
	}
	
	@RequestMapping("/userLogin")
	public String userLogin(UserLogin ul,HttpServletRequest request){
		System.out.println("........"+ul.getUphone()+ul.getUpassword());
		Boolean i=ui.userLogin(ul);	
		if(i==true)
		return "redirect:/loginSuc.jsp";
		else return "regist1.jsp";
	}
	

	@RequestMapping("/usergetMerchant")
	@ResponseBody
	public List<Merchant> getMerchant(){
	
		
		return ui.getMerchant();
		
	}
	
	@RequestMapping("/sendOrder")
	public void sendOrder(Order o,HttpServletRequest request){
		ui.sendOrder(o);
		
	}
	

}
